package com.exmaple.lemlin.ui.activity;

import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.exmaple.lemlin.R;
import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.app.Constants;
import com.exmaple.lemlin.callback.OnItemClickFriend;
import com.exmaple.lemlin.entity.FriendGood;
import com.exmaple.lemlin.mvp.presenter.FriendPresenter;
import com.exmaple.lemlin.mvp.view.IFriendView;
import com.exmaple.lemlin.ui.abstracts.AbstractBaseActivity;
import com.exmaple.lemlin.ui.fragment.FriendAdditionFragment;
import com.exmaple.lemlin.ui.fragment.FriendSearchFragment;
import com.exmaple.lemlin.utils.LogUtils;
import com.exmaple.lemlin.xmpp.XmppConnection;

import org.jivesoftware.smack.packet.Presence;

import java.util.List;

import butterknife.BindView;

public class FriendActivity extends AbstractBaseActivity<FriendPresenter> implements IFriendView, OnItemClickFriend {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private SearchView searchView;

    private FriendGood friend;

    private OnFriendSearchListener mOnFriendSearchListener = null;
    private OnFriendAdditionListener mOnFriendAdditionListener = null;

    @Override
    public void onAttachFragment(Fragment fragment) {
        try {
            if (fragment instanceof FriendSearchFragment) {
                mOnFriendSearchListener = (OnFriendSearchListener) fragment;
            } else if (fragment instanceof FriendAdditionFragment) {
                mOnFriendAdditionListener = (OnFriendAdditionListener) fragment;
            }
        } catch (Exception e) {
            throw new ClassCastException(this.toString() + " must implement OnMainListener");
        }
        super.onAttachFragment(fragment);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_friend;
    }

    @Override
    protected void initView() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.replace(R.id.frame_layout, FriendAdditionFragment.newInstance());
        ft.commit();
    }

    @Override
    protected void initPresenter() {
        mPresenter = new FriendPresenter((this), (this));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (!searchView.isIconified()) {
            searchView.setQuery((null), (false));
            searchView.setIconified(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.top_friend_nav_menu, menu);
        searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setQueryHint("用户名...");
        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.add(R.id.frame_layout, FriendSearchFragment.newInstance());
                ft.addToBackStack(null);
                ft.commit();
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                mPresenter.userSearch(s);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                getSupportFragmentManager().popBackStackImmediate();
                return false;
            }
        });
        return true;
    }

    @Override
    public void onFriendGoods(List<FriendGood> friendGoods) {
        if (mOnFriendSearchListener != null)
            mOnFriendSearchListener.onFriendGoods(friendGoods);
    }

    @Override
    public void onSuccessAddSub() {
        friend.setSub("both");
        APPIM.getDaoSession().getFriendGoodDao().update(friend);
        if (mOnFriendAdditionListener != null) {
            mOnFriendAdditionListener.onFriendGood(friend);
        }
    }

    @Override
    public void onItemClickAddition(View view, int position, final FriendGood friend) {
        this.friend = friend;
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage("是否同意并添加用户【" + friend.getName() + "】到联系人")
                .setPositiveButton("同意并添加", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.subscribedAddCreateFriend(friend.getName());
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).create();
        dialog.show();
    }

    @Override
    public void onItemClickSearch(View view, int position, final FriendGood friend) {
        this.friend = friend;
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage("是否添加用户【" + friend.getName() + "】到联系人")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.addCreateFriend(friend.getName());
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).create();
        dialog.show();
    }


    public interface OnFriendSearchListener {
        void onFriendGoods(List<FriendGood> friendGoods);
    }

    public interface OnFriendAdditionListener {
        void onFriendGood(FriendGood friendGood);
    }
}
